package net.vanchu.mortgagecalculator.mortgage.resultShow;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dtspread.apps.houseloan.R;

public class ResultShowActivity extends AppCompatActivity implements ResultShowContract.View {

    private static final String TAG = ResultShowActivity.class.getSimpleName();

    private ViewPager vpWrapTwoResult;

    private TextView tvLeftTitle;

    private TextView tvShowTotalNumberLeft;

    private TextView tvRightTitle;

    private TextView tvShowTotalNumberRight;

    private View lineAccentLeft;

    private View lineNormalLeft;

    private View lineAccentRight;

    private View lineNormalRight;

    private ImageView imgBack;

    private TextView tvSetRepayRemind;

    private TextView tvTotalPrincipal;

    private ResultShowContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_show);

        tvTotalPrincipal = (TextView) findViewById(R.id.result_show_tv_total_principal);

        imgBack = (ImageView) findViewById(R.id.result_show_img_back);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResultShowActivity.this.onBackPressed();
            }
        });

        tvSetRepayRemind = (TextView) findViewById(R.id.result_show_tv_set_repay_remind);
        tvSetRepayRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEntranceForSettingRepayRemind();
            }
        });

        vpWrapTwoResult = (ViewPager) findViewById(R.id.result_show_vp_wrap_two_result);
        vpWrapTwoResult.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    turnToTitleLeft();
                    turnToPageLeft();
                } else {
                    turnToTitleRight();
                    turnToPageRight();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tvLeftTitle = (TextView) findViewById(R.id.result_show_tv_left_title);
        tvRightTitle = (TextView) findViewById(R.id.result_show_tv_right_title);
        tvShowTotalNumberLeft = (TextView) findViewById(R.id.result_show_tv_show_total_number_left);
        tvShowTotalNumberRight = (TextView) findViewById(R.id.result_show_tv_show_total_number_right);
        lineAccentLeft = findViewById(R.id.result_show_line_accent_left);
        lineNormalLeft = findViewById(R.id.result_show_line_normal_left);
        lineAccentRight = findViewById(R.id.result_show_line_accent_right);
        lineNormalRight = findViewById(R.id.result_show_line_normal_right);
        LinearLayout resultShowAreaLeft = (LinearLayout) findViewById(R.id.result_show_area_left);
        LinearLayout resultShowAreaRight = (LinearLayout) findViewById(R.id.result_show_area_right);
        resultShowAreaLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnToTitleLeft();
                turnToPageLeft();
            }
        });
        resultShowAreaRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnToTitleRight();
                turnToPageRight();
            }
        });
        presenter = new ResultShowPresenter(this, new ComplainButtonListener() {
            @Override
            public void onClickComplainButton() {
                presenter.shareResult(ResultShowActivity.this);
            }
        });
        if (savedInstanceState == null) {
            presenter.extractData(getIntent());
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        presenter.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        presenter.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void showSingleLoanResult(SingleWayResultDataAdapter[] dataAdapters, String leftTotalValueToShow, String rightTotalValueToShow) {
        tvShowTotalNumberLeft.setText(leftTotalValueToShow);
        tvShowTotalNumberRight.setText(rightTotalValueToShow);
        SingleWayPageAdapter singleWayPageAdapter = new SingleWayPageAdapter(createRecyclerViews(), dataAdapters);
        vpWrapTwoResult.setAdapter(singleWayPageAdapter);
    }

    @Override
    public void showMixLoanResult(MixWayResultDataAdapter[] dataAdapters, String leftTotalValueToShow, String rightTotalValueToShow) {
        tvShowTotalNumberLeft.setText(leftTotalValueToShow);
        tvShowTotalNumberRight.setText(rightTotalValueToShow);
        MixWayPageAdapter mixWayPageAdapter = new MixWayPageAdapter(createRecyclerViews(), dataAdapters);
        vpWrapTwoResult.setAdapter(mixWayPageAdapter);
    }

    @Override
    public void showTotalPrincipal(String principalStrToShow) {
        tvTotalPrincipal.setText(principalStrToShow);
    }

    private void turnToPageLeft() {
        vpWrapTwoResult.setCurrentItem(0);
    }

    private void turnToPageRight() {
        vpWrapTwoResult.setCurrentItem(1);
    }

    private void turnToTitleLeft() {
        lineAccentLeft.setVisibility(View.VISIBLE);
        lineAccentRight.setVisibility(View.INVISIBLE);
        lineNormalLeft.setVisibility(View.VISIBLE);
        lineNormalRight.setVisibility(View.VISIBLE);
        tvRightTitle.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_normal));
        tvShowTotalNumberRight.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_normal));
        tvLeftTitle.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_accent));
        tvShowTotalNumberLeft.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_accent));
    }

    private void turnToTitleRight() {
        lineAccentLeft.setVisibility(View.INVISIBLE);
        lineAccentRight.setVisibility(View.VISIBLE);
        lineNormalLeft.setVisibility(View.VISIBLE);
        lineNormalRight.setVisibility(View.VISIBLE);
        tvLeftTitle.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_normal));
        tvShowTotalNumberLeft.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_normal));
        tvRightTitle.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_accent));
        tvShowTotalNumberRight.setTextColor(getResources().getColor(R.color.txt_result_show_repay_way_accent));
    }

    private void showEntranceForSettingRepayRemind() {
        final Dialog dialog = new Dialog(ResultShowActivity.this, R.style.CustomDialog);
        dialog.setContentView(R.layout.layout_set_repay_remind);
        TextView cancel = (TextView) dialog.findViewById(R.id.set_repay_remind_tv_cancel);
        TextView ensure = (TextView) dialog.findViewById(R.id.set_repay_remind_tv_ensure);
        final EditText setRepayRemindEdtDate = (EditText) dialog.findViewById(R.id.set_repay_remind_edt_date);
        setRepayRemindEdtDate.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        final StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(dest, 0, dstart);
                        stringBuilder.append(source, start, end);
                        stringBuilder.append(dest, dend, dest.length());
                        String nextState = stringBuilder.toString();
                        if (nextState.length() == 0) {
                            return null;
                        }
                        if (!TextUtils.isDigitsOnly(nextState)) {
                            notifyInputError("请输入一个 1~30 之间的整数");
                            return dest.subSequence(dstart, dend);
                        }
                        int nextInt = Integer.parseInt(nextState);
                        if (nextInt > 30 || nextInt < 1) {
                            notifyInputError("请输入一个 1~30 之间的整数");
                            return dest.subSequence(dstart, dend);
                        }
                        return null;
                    }
                }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = setRepayRemindEdtDate.getText().toString();
                if (TextUtils.isEmpty(input)) {
                    Toast.makeText(ResultShowActivity.this, R.string.please_input_date, Toast.LENGTH_SHORT).show();
                    return;
                }
                int date = Integer.parseInt(input);
                presenter.performSetRemindDate(date);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void notifyInputError(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private RecyclerView[] createRecyclerViews() {
        return new RecyclerView[]{createRecyclerView(), createRecyclerView()};
    }

    private RecyclerView createRecyclerView() {
        RecyclerView recyclerView = new RecyclerView(this);
        recyclerView.setLayoutParams(new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        return recyclerView;
    }
}